package com.seguriapp.administration.gwt.client.activity;

import net.customware.gwt.dispatch.client.DispatchAsync;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.seguriapp.administration.gwt.client.place.UserListsPlace;
import com.seguriapp.administration.gwt.shared.SaveUserDataAction;
import com.seguriapp.administration.gwt.shared.SaveUserDataResult;
import com.seguriapp.common.gwt.client.AbstractSeguriAppActivity;
import com.seguriapp.common.gwt.client.AbstractSeguriAppPlace;
import com.seguriapp.common.gwt.client.SeguriAppCallback;
import com.seguriapp.common.gwt.client.SeguriAppClientSession;
import com.seguriapp.common.gwt.client.SeguriAppDisplay;
import com.seguriapp.entities.User;

public class EditUserDataActivity extends AbstractSeguriAppActivity {

  private final Display display;
  private User user;
  private boolean isNewUser;

  public EditUserDataActivity(final DispatchAsync dispatchAsync, final EventBus eventBus, final PlaceController placeController,
      final Display display, final AbstractSeguriAppPlace place, final SeguriAppClientSession clientSession) {
    super(dispatchAsync, eventBus, placeController, place, clientSession);
    display.setPresenter(this);
    this.display = display;
  }

  @Override
  protected void onStart(final AcceptsOneWidget widget, final EventBus eventBus) {
    widget.setWidget(display.asWidget());
    final Object isNewUserFlag = clientSession.get(SeguriAppClientSession.Key.isNewUser);
    if (isNewUserFlag != null && isNewUserFlag instanceof Boolean) {
      isNewUser = (Boolean) isNewUserFlag;
      if (isNewUser) {
        display.setNewUserForm();
      } else {
        final Object userForm = clientSession.get(SeguriAppClientSession.Key.User);
        if (userForm != null && userForm instanceof User) {
          user = (User) userForm;
          display.setEditUserForm(user);
        } else {
          display.showErrorMessage();
        }
      }
    } else {
      display.showErrorMessage();
    }
  }

  public void saveUser(final String username, final String password) {
    final SaveUserDataAction action = new SaveUserDataAction();
    final User userForm = new User();
    userForm.setUsername(username);
    userForm.setPassword(password);
    userForm.setId(user == null ? null : user.getId());
    action.setUser(userForm);
    dispatchAsync.execute(action, new SeguriAppCallback<SaveUserDataResult>() {

      public void onSuccess(final SaveUserDataResult result) {
        if (result.getErrorMessage() != null) {
          display.setErrorMessage(result.getErrorMessage());
        } else {
          goToUsersListPage();
        }
      }

    });
  }

  private void goToUsersListPage() {
    final Place place = new UserListsPlace("");
    placeController.goTo(place);
  }

  public interface Display extends SeguriAppDisplay {

    void setPresenter(EditUserDataActivity presenter);

    void setErrorMessage(String errorMessage);

    void setEditUserForm(User user);

    void setNewUserForm();

    void showErrorMessage();

  }

}
